/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.coupon.web.CouponSearchAction
   Module Description   :

   Date Created      : 2009/12/14
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.coupon.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.common.web.AbstractAction;
import com.yaodian100.ec.coupon.entity.Coupon;
import com.yaodian100.ec.coupon.entity.Coupon.ApplyRangType;
import com.yaodian100.ec.coupon.entity.Coupon.Status;
import com.yaodian100.ec.coupon.service.CouponService;

/**
 * @author jeff.ma
 * 
 */
public class CouponSearchAction extends AbstractAction {

	/** serialVersionUID */
	private static final long serialVersionUID = 3442840197718883894L;
	@Resource(name = "couponService")
	private CouponService couponService;
	@Resource(name = "menuService")
	private MenuService menuService;

	private List<Coupon> coupons;

	/* 由FORM传入之查询条件 */
	private String paramApplyRangType;
	private String paramStatus;
	private String paramName;
	private String paramBudget;

	/* prepare menu */
	private Menu statusMenu;
	private Menu applyRangTypeMenu;
	private Menu menuBudget;

	/** default constructor */
	public CouponSearchAction() {
	}

	/**
	 * @return the statusMenu
	 */
	public Menu getStatusMenu() {
		return statusMenu;
	}

	/**
	 * @param statusMenu the statusMenu to set
	 */
	public void setStatusMenu(Menu statusMenu) {
		this.statusMenu = statusMenu;
	}

	/**
	 * @return the applyRangTypeMenu
	 */
	public Menu getApplyRangTypeMenu() {
		return applyRangTypeMenu;
	}

	/**
	 * @param applyRangTypeMenu the applyRangTypeMenu to set
	 */
	public void setApplyRangTypeMenu(Menu applyRangTypeMenu) {
		this.applyRangTypeMenu = applyRangTypeMenu;
	}

	/**
	 * @return the coupons
	 */
	public List<Coupon> getCoupons() {
		return coupons;
	}

	/**
	 * @param coupons the coupons to set
	 */
	public void setCoupons(List<Coupon> coupons) {
		this.coupons = coupons;
	}

	/**
	 * @return the paramApplyRangType
	 */
	public String getParamApplyRangType() {
		return paramApplyRangType;
	}

	/**
	 * @param paramApplyRangType the paramApplyRangType to set
	 */
	public void setParamApplyRangType(String paramApplyRangType) {
		this.paramApplyRangType = paramApplyRangType;
	}

	/**
	 * @return the paramStatus
	 */
	public String getParamStatus() {
		return paramStatus;
	}

	/**
	 * @param paramStatus the paramStatus to set
	 */
	public void setParamStatus(String paramStatus) {
		this.paramStatus = paramStatus;
	}

	/**
	 * @return the paramName
	 */
	public String getParamName() {
		return paramName;
	}

	/**
	 * @param paramName the paramName to set
	 */
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		clearActionErrors();
		if (statusMenu == null) {
			statusMenu = menuService.get("Coupon.status");
		}
		if (applyRangTypeMenu == null) {
			applyRangTypeMenu = menuService.get("Coupon.applyRangType");
		}
		if (menuBudget == null) {
			menuBudget = menuService.get("Budget");
		}
	}

	public String list() {
		return "list";
	}

	public String search() {
		CommonCriteria cri = new CommonCriteria();
		getPager().setPageRecord(20);
		try {
			if (StringUtils.isNotBlank(paramName)) {
				cri.addRlike("name", paramName);
			}
			if (StringUtils.isNotBlank(paramApplyRangType)) {
				cri.addEq("applyRangeType", ApplyRangType.valueOf(ApplyRangType.class, paramApplyRangType));
			}
			if (StringUtils.isNotBlank(paramStatus)) {
				cri.addEq("status", Status.valueOf(Status.class, paramStatus));
			}
			if (StringUtils.isNotBlank(paramBudget)) {
				cri.addEq("budget", paramBudget);
			}

			long entitySize = couponService.getListSize(cri).longValue();
			getPager().setTotalSize(entitySize);
			coupons = couponService.getList(getPager().getCurrentPage() * getPager().getPageRecord(), getPager()
					.getPageRecord(), cri, null);
			logger.debug("pager:{}", getPager());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}

	public Menu getMenuBudget() {
		return menuBudget;
	}

	public void setMenuBudget(Menu menuBudget) {
		this.menuBudget = menuBudget;
	}

	public String getParamBudget() {
		return paramBudget;
	}

	public void setParamBudget(String paramBudget) {
		this.paramBudget = paramBudget;
	}
}
